package cn.sdut.training.web.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import cn.sdut.training.domain.Oj;
import cn.sdut.training.domain.User;
import cn.sdut.training.domain.UserAccount;
import cn.sdut.training.service.TrainingService;
import cn.sdut.training.service.UserService;

public class UserAccountAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 5292201046789804393L;
	
	private Map<String, Object> session;
	private List<UserAccount> accountList;//当前登录用户的account列表
	private UserAccount userAccount;
	private List<Oj> ojList;//练习网站列表
	private Long ojId;//新增加的账号的ojId
	private Long id;//userAccountId，用于编辑
	private Long[] idList;//用于批量删除
	
	@Resource
	private UserService userService;
	
	@Resource
	private TrainingService trainingService;
	
	/**
	 * 转向添加用户账户页面
	 */
	public String toSavePage(){
		ojList = trainingService.findAllOj();
		return "toSavePage";
	}
	
	/**
	 * 转向用户账号列表页面
	 */
	public String toListPage(){
		User user = (User) session.get("user");
		if(user!=null){
			accountList = userService.getUserAccountListByUserId(user.getId());
		}
		return "toListPage";
	}
	
	/**
	 * 执行添加账号操作
	 */
	public String save(){
		Oj oj = trainingService.getOjById(ojId);
		userAccount.setOj(oj);
		User user = (User) session.get("user");
		userAccount.setUser(user);
		userService.saveUserAccount(userAccount);
		return "toList";
	}
	
	/**
	 * 检查该网站是否已经配置了账号
	 */
	public void validateSave(){
		ojList = trainingService.findAllOj();
		User user = (User) session.get("user");
		accountList = userService.getUserAccountListByUserId(user.getId());
		for(UserAccount ua:accountList){
			if (ua.getOj().getId().equals(ojId)) {//
				addFieldError("ojId", "注意：你已经配置了该网站的账号，请不要重复配置！");
			}
		}
	}
	
	/**
	 * 转向编辑页面
	 * @return
	 */
	public String toEditPage(){
		ojList = trainingService.findAllOj();
		userAccount = userService.getUserAccountById(id);
		return "toEditPage";
	}
	
	/**
	 * 处理更新操作
	 * @return
	 */
	public String edit() {
		Oj oj = trainingService.getOjById(ojId);
		userAccount.setOj(oj);
		User user = (User) session.get("user");
		userAccount.setUser(user);
		userService.updateUserAccount(userAccount);
		return "toList";
	}
	
	/**
	 * 处理删除操作
	 * @return
	 */
	public String delete(){
		for(Long id:idList){
			userService.deleteUserAccount(id);
		}
		return "toList";
	}
	
	
	//---------------------------------------------------
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<UserAccount> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<UserAccount> accountList) {
		this.accountList = accountList;
	}

	public UserAccount getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(UserAccount userAccount) {
		this.userAccount = userAccount;
	}

	public List<Oj> getOjList() {
		return ojList;
	}

	public void setOjList(List<Oj> ojList) {
		this.ojList = ojList;
	}

	public Long getOjId() {
		return ojId;
	}

	public void setOjId(Long ojId) {
		this.ojId = ojId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIdList() {
		return idList;
	}

	public void setIdList(Long[] idList) {
		this.idList = idList;
	}
}
